package com.android.aiod.bean;

import com.android.aiod.manager.ConfigManager;
import com.android.baselibrary.tool.Log;
import com.blankj.utilcode.util.ConvertUtils;
import com.licheedev.hwutils.ByteUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//串口数据-父类
public class SerailDataInfo_Base {

    public byte[] bytes_head = new byte[4];    //头部固定4个字节  FF FF （START 报文开始标志） 01 （VER 协议版本号） 15  （SEQ 序列号）
    public byte[] bytes_length = new byte[4];  //长度字段
    public byte[] bytes_data;    //Date数据
    public byte[] bytes_code;    //Code数据，文本内容
    public byte[] bytes_CRC;    //CRC校验

    public byte[] byte_CMD;
    public byte[] byte_transparency;    //文字透明度
    public byte[] byte_color;           //文字颜色
    public byte[] byte_voice;           //语音音量

    public byte error;          //错误信息

    //获取指令类型
    public int getCMDType() {
        int type = 0;
        switch (getCMD_Str()) {
            case "A3":
                type = 0;
                break;
            case "A4":
                type = 1;
                break;
            case "A6":
                type = 2;
                break;
        }
        return type;
    }

    //获取数据长度
    public int getDataLength() {
        return (int) ByteUtil.bytes2long(bytes_length);
    }

    //获取CMD字符
    public String getCMD_Str() {
        String str = ConvertUtils.bytes2HexString(byte_CMD);
        return str;
    }

    //获取文本内容-文字列表
    public List<String> getCodeStr_TextList() {
        List<String> textList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            byte[] bytes_row = new byte[20];   //一共6行，每行20字节
            System.arraycopy(bytes_code, i * 20, bytes_row, 0, 20);
            String text = ConvertUtils.bytes2String(bytes_row, "GB2312");
            textList.add(text);
        }
        return textList;
    }

    //获取文本内容-语音文字
    public String getCodeStr_TextVoice() {
        String text = ConvertUtils.bytes2String(bytes_code, "GB2312");
        return text;
    }

    //获取文字颜色
    public String getTextColor() {
        String str = ConvertUtils.bytes2HexString(byte_color);
        String colorStr = "";
        switch (str) {
            case "01":   //绿色
                colorStr = "#00FF00";
                break;
            case "02":   //红色
                colorStr = "#FF0000";
                break;
            case "03":
                colorStr = "#FFFF00";
                break;
            case "00":
                colorStr = ConfigManager.instance().configInfo.text_color;
                break;
        }
        return colorStr;
    }

    //获取透明度
    public float getAlpha() {
        String[] arr = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F"};
        String alphaStr = ConvertUtils.bytes2HexString(byte_transparency);
        int index = Arrays.asList(arr).indexOf(alphaStr);
        float alpha = (float) index / ((float) arr.length -1);
        if ("00".equals(alphaStr)) {
            alpha = 1;
        } else if ("01".equals(alphaStr)) {
            alpha = 0;
        }
        return alpha;
    }

    //获取音量
    public int getMediaVolume() {
        String[] arr = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09"};
        String volumeStr = ConvertUtils.bytes2HexString(byte_voice);
        int index = Arrays.asList(arr).indexOf(volumeStr);
        float temp = (float) index / ((float) arr.length -1);
        int volume =  (int )(temp * 15);
        if ("00".equals(volumeStr)) {
            volume = 15;
        } else if ("01".equals(volumeStr)) {
            volume = 0;
        }
        return volume;
    }

}
